﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T.SF.Console
{
    /// <summary>
    /// 本地错误日志服务
    /// </summary>
    static class LocalErrorLogService
    {
        private static readonly object syncObj = new object();

        /// <summary>
        /// 记录异常信息
        /// </summary>
        /// <param name="er">异常信息</param>
        public static void Write(System.Exception er)
        {
            lock (syncObj)
            {
                if (er == null)
                    return;
                string errPath = string.Empty;
                try
                {
                    string path = GetPath();
                    if (Directory.Exists(path) == false)
                        Directory.CreateDirectory(path);
                    path = Path.Combine(path, string.Format("error-{0}.txt", DateTime.Now.ToString("yyyyMMdd")));
                    errPath = path;
                    StringBuilder sb = new StringBuilder(100);
                    sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "=========================================================");
                    sb.AppendLine("Message:" + er.Message);
                    sb.AppendLine("Full Stack:" + er.ToString());
                    if (er.Data != null)
                    {
                        sb.AppendLine("Data:");
                        foreach (var item in er.Data.Keys)
                        {
                            sb.AppendLine("  " + item + " = " + Convert.ToString(er.Data[item]));
                        }
                    }

                    var inner = er.InnerException;
                    while (inner != null)
                    {
                        sb.AppendLine("InnerException Message:" + inner.Message);
                        sb.AppendLine("InnerException Stack:" + inner.StackTrace);
                        if (inner.Data != null)
                        {
                            sb.AppendLine("InnerException Data:");
                            foreach (var item in inner.Data.Keys)
                            {
                                sb.AppendLine("  " + item + " = " + Convert.ToString(inner.Data[item]));
                            }
                        }

                        inner = inner.InnerException;
                    }

                    File.AppendAllText(path, sb.ToString());
                }
                catch (System.Exception err)
                {
                    if (string.IsNullOrEmpty(errPath) == false)
                    {
                        try
                        {
                            StringBuilder sb = new StringBuilder(100);
                            sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "=========================================================");
                            sb.AppendLine("Message:" + err.Message);
                            sb.AppendLine("Stack:" + err.ToString());
                            File.AppendAllText(errPath, sb.ToString());
                        }
                        catch
                        { }
                    }
                }
            }
        }

        private static string GetPath()
        {
            return Path.Combine(System.Environment.CurrentDirectory, "log");
        }
    }
}
